How to convert int[] into list in java? 您所在的位置:网站首页 数据库 int integer How to convert int[] into list in java?

How to convert int[] into list in java?

2023-03-30 08:33| 来源: 网络整理| 查看: 265

Java provides several ways to convert an array of primitive int type into a List of Integer objects. One common use case for this is when a method requires a List as an argument, but the data you have is in the form of an array. Additionally, converting an array to a List allows for the use of more advanced operations, such as sorting and filtering, that are not available for arrays.

Method 1: Using Arrays.asList()

To convert an int[] array into a List in Java using Arrays.asList(), you can follow these steps:

Declare and initialize an int[] array: int[] intArray = {1, 2, 3, 4, 5}; Convert the int[] array to a List using Arrays.asList(): List intList = Arrays.asList(intArray);

Here is the complete code example:

int[] intArray = {1, 2, 3, 4, 5}; List intList = Arrays.asList(intArray);

Note that if you try to modify the List using add(), remove(), or clear() methods, you will get an UnsupportedOperationException because Arrays.asList() returns a fixed-size list backed by the original array.

To avoid this, you can create a new ArrayList from the List returned by Arrays.asList():

int[] intArray = {1, 2, 3, 4, 5}; List intList = new ArrayList(Arrays.asList(intArray));

Now you can modify the intList as usual.

Here is another example that shows how to convert a subset of an int[] array to a List:

int[] intArray = {1, 2, 3, 4, 5}; List intList = Arrays.asList(Arrays.copyOfRange(intArray, 1, 4));

This code creates a new int[] array containing the elements from index 1 to index 3 (inclusive) of the original intArray, and then converts it to a List using Arrays.asList().

Method 2: Using Stream.of() and Collectors.toList()

To convert an int[] array into a List in Java using Stream.of() and Collectors.toList(), follow these steps:

Create an int[] array. int[] intArray = {1, 2, 3, 4, 5}; Use Stream.of() to convert the int[] array into a Stream. Stream stream = Stream.of(intArray.boxed()); Use Collectors.toList() to collect the Stream into a List. List list = stream.collect(Collectors.toList());

The final code will look like this:

int[] intArray = {1, 2, 3, 4, 5}; Stream stream = Stream.of(intArray.boxed()); List list = stream.collect(Collectors.toList());

You can also combine the above steps into a single line of code:

List list = Arrays.stream(intArray).boxed().collect(Collectors.toList());

In this case, Arrays.stream() is used to convert the int[] array into an IntStream, which is then boxed using the boxed() method to convert it into a Stream. The Collectors.toList() method is then used to collect the Stream into a List.

Overall, using Stream.of() and Collectors.toList() is a simple and concise way to convert an int[] array into a List in Java.

Method 3: Using a loop and ArrayList's add() method

To convert an int[] into a List in Java using a loop and ArrayList's add() method, follow these steps:

Create an empty ArrayList to hold the converted int values. Loop through the int[] using a for loop. Inside the loop, use the Integer.valueOf() method to convert the int value to an Integer object. Add the Integer object to the ArrayList using the add() method.

Here's an example code snippet that demonstrates the above steps:

int[] intArray = {1, 2, 3, 4, 5}; List intList = new ArrayList(); for (int i = 0; i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有